package com.qqtech.qquan.pub.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.pub.cache.PubSysParamCache;
import com.qqtech.qquan.pub.cache.PubUpgradeCache;
import com.qqtech.qquan.pub.dao.PubSysParamDao;
import com.qqtech.qquan.pub.dao.PubUpgradeDao;
import com.qqtech.qquan.pub.enums.PubConst;
import com.qqtech.qquan.pub.enums.PubPlatformEnum;
import com.qqtech.qquan.pub.model.PubSysParam;
import com.qqtech.qquan.pub.model.PubUpgrade;
import com.qqtech.qquan.pub.model.PubUpgradeVo;

@Service
public class PubUpgradeServiceImpl extends BaseServiceImpl<PubUpgrade>implements PubUpgradeService {
	public static final Logger log = LoggerFactory.getLogger(PubUpgradeServiceImpl.class);

	@Resource
	private PubUpgradeDao pubUpgradeDao;

	@Resource
	private PubUpgradeCache pubUpgradeCache;
	@Resource
	private PubSysParamCache pubSysParamCache;
	@Resource
	private PubSysParamDao pubSysParamDao;

	@Override
	protected BaseDao<PubUpgrade> getBaseDao() {
		return pubUpgradeDao;
	}

	@Override
	public Result publishNewVersion(PubUpgrade pubUpgrade) {
		Result result = new Result();
		updatePubSysParam(pubUpgrade);
		pubUpgradeDao.insert(pubUpgrade);
		pubUpgradeCache.clearAll();
		return result;
	}

	@Override
	public PubUpgrade getCachePubUpgrade() {
		return pubUpgradeCache.getCachePubUpgrade();
	}

	@Override
	public PubUpgrade getFromParam() {
		PubUpgrade pubUpgrade = new PubUpgrade();
		pubUpgrade.setIsUpgrade(
				pubSysParamCache.getInt(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isUpgrade", YesNoEnum.NO.getCode()));
		pubUpgrade.setIsRestart(pubSysParamCache.getInt(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isRestart"));
		pubUpgrade.setIsSilence(pubSysParamCache.getInt(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isSilence"));
		pubUpgrade.setIsWebCheck(pubSysParamCache.getInt(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isWebCheck"));

		pubUpgrade.setTitle(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "title"));
		pubUpgrade.setContent(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "content"));

		pubUpgrade.setNativeVersion(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "nativeVersion"));
		pubUpgrade.setNativeBuild(pubSysParamCache.getInt(PubConst.PARAM_PUB_UPGRADE_PRE_ + "nativeBuild"));

		pubUpgrade.setAndroidMarketFile(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "androidMarketFile"));
		pubUpgrade.setAndroidVersion(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "androidVersion"));
		pubUpgrade.setAndroidUpdFile(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "androidUpdFile"));

		pubUpgrade.setIosMarketFile(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "iosMarketFile"));
		pubUpgrade.setIosVersion(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "iosVersion"));
		pubUpgrade.setIosUpdFile(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "iosUpdFile"));
		pubUpgrade.setLeastNativeVersion(pubSysParamCache.get(PubConst.PARAM_PUB_UPGRADE_PRE_ + "leastNativeVersion"));
		return pubUpgrade;
	}

	public void updatePubSysParam(PubUpgrade pubUpgrade) {
		PubSysParam isUpgrade = new PubSysParam();
		isUpgrade.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isUpgrade");
		isUpgrade.setValue(String.valueOf(pubUpgrade.getIsUpgrade()));
		pubSysParamDao.updateByKey(isUpgrade);

		PubSysParam isRestart = new PubSysParam();
		isRestart.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isRestart");
		isRestart.setValue(String.valueOf(pubUpgrade.getIsRestart()));
		pubSysParamDao.updateByKey(isRestart);

		PubSysParam isSilence = new PubSysParam();
		isSilence.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isSilence");
		isSilence.setValue(String.valueOf(pubUpgrade.getIsSilence()));
		pubSysParamDao.updateByKey(isSilence);

		PubSysParam isWebCheck = new PubSysParam();
		isWebCheck.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isWebCheck");
		isWebCheck.setValue(String.valueOf(pubUpgrade.getIsWebCheck()));
		pubSysParamDao.updateByKey(isWebCheck);

		PubSysParam isNeedPush = new PubSysParam();
		isNeedPush.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isNeedPush");
		pubSysParamDao.updateByKey(isNeedPush);

		PubSysParam title = new PubSysParam();
		title.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "title");
		title.setValue(String.valueOf(pubUpgrade.getTitle()));
		pubSysParamDao.updateByKey(title);

		PubSysParam content = new PubSysParam();
		content.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "content");
		content.setValue(String.valueOf(pubUpgrade.getContent()));
		pubSysParamDao.updateByKey(content);

		PubSysParam nativeVersion = new PubSysParam();
		nativeVersion.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "nativeVersion");
		nativeVersion.setValue(String.valueOf(pubUpgrade.getNativeVersion()));
		pubSysParamDao.updateByKey(nativeVersion);

		PubSysParam nativeBuild = new PubSysParam();
		nativeBuild.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "nativeBuild");
		nativeBuild.setValue(String.valueOf(pubUpgrade.getNativeBuild()));
		pubSysParamDao.updateByKey(nativeBuild);

		PubSysParam androidMarketFile = new PubSysParam();
		androidMarketFile.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "androidMarketFile");
		androidMarketFile.setValue(String.valueOf(pubUpgrade.getAndroidMarketFile()));
		pubSysParamDao.updateByKey(androidMarketFile);

		PubSysParam androidVersion = new PubSysParam();
		androidVersion.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "androidVersion");
		androidVersion.setValue(String.valueOf(pubUpgrade.getAndroidVersion()));
		pubSysParamDao.updateByKey(androidVersion);

		PubSysParam androidUpdFile = new PubSysParam();
		androidUpdFile.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "androidUpdFile");
		androidUpdFile.setValue(String.valueOf(pubUpgrade.getAndroidUpdFile()));
		pubSysParamDao.updateByKey(androidUpdFile);

		PubSysParam iosMarketFile = new PubSysParam();
		iosMarketFile.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "iosMarketFile");
		iosMarketFile.setValue(String.valueOf(pubUpgrade.getIosMarketFile()));
		pubSysParamDao.updateByKey(iosMarketFile);

		PubSysParam iosVersion = new PubSysParam();
		iosVersion.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "iosVersion");
		iosVersion.setValue(String.valueOf(pubUpgrade.getIosVersion()));
		pubSysParamDao.updateByKey(iosVersion);

		PubSysParam iosUpdFile = new PubSysParam();
		iosUpdFile.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "iosUpdFile");
		iosUpdFile.setValue(String.valueOf(pubUpgrade.getIosUpdFile()));
		pubSysParamDao.updateByKey(iosUpdFile);

		PubSysParam leastNativeVersion = new PubSysParam();
		leastNativeVersion.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "leastNativeVersion");
		leastNativeVersion.setValue(String.valueOf(pubUpgrade.getLeastNativeVersion()));
		pubSysParamDao.updateByKey(leastNativeVersion);

		PubSysParam isUpdContentOnly = new PubSysParam();
		isUpdContentOnly.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "isUpdContentOnly");
		isUpdContentOnly.setValue(String.valueOf(pubUpgrade.getIsUpdContentOnly()));
		pubSysParamDao.updateByKey(isUpdContentOnly);

		PubSysParam svnAppVersion = new PubSysParam();
		svnAppVersion.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "svnAppVersion");
		svnAppVersion.setValue(String.valueOf(pubUpgrade.getSvnAppVersion()));
		pubSysParamDao.updateByKey(svnAppVersion);

		PubSysParam svnServerVersion = new PubSysParam();
		svnServerVersion.setKey(PubConst.PARAM_PUB_UPGRADE_PRE_ + "svnServerVersion");
		svnServerVersion.setValue(String.valueOf(pubUpgrade.getSvnServerVersion()));
		pubSysParamDao.updateByKey(svnServerVersion);
		pubSysParamCache.clearAll();
	}

	@Override
	public PubUpgrade getCurrentUpgrade() {
		PubUpgrade pubUpgrade = new PubUpgrade();
		List<PubUpgrade> listPubUpgrade = pubUpgradeDao.queryList(pubUpgrade,
				new PageKit(1, 1, new PageOrder(Direction.DESC, "id")));
		if (listPubUpgrade != null && listPubUpgrade.size() > 0) {
			return listPubUpgrade.get(0);
		}
		pubUpgrade.setIsUpgrade(YesNoEnum.NO.getCode());
		pubUpgrade.setIsWebCheck(YesNoEnum.NO.getCode());
		return pubUpgrade;
	}

	@Override
	public Result checkUpgrade(String contentVer, String nativeVer, int platform) {
		Result result = new Result();
		if (!PubPlatformEnum.isInScope(platform)) {
			result.addErr("当前版本不需要升级");
		}
		PubUpgrade pubUpgrade = this.getCachePubUpgrade();
		if (pubUpgrade == null) {
			result.addErr("当前版本不需要升级");
		}
		if (YesNoEnum.YES.getCode() != pubUpgrade.getIsUpgrade()) {
			result.addErr("当前版本不需要升级");
		}
		if (!result.checkForm()) {
			return result;
		}
		String nativeVersion = pubUpgrade.getNativeVersion();
		String leastNativeVersion = pubUpgrade.getLeastNativeVersion();
		String updVersion = pubUpgrade.getAndroidVersion();
		if (platform == PubPlatformEnum.ANDROID.getCode()) {
			updVersion = pubUpgrade.getAndroidVersion();
		} else if (platform == PubPlatformEnum.IOS.getCode()) {
			updVersion = pubUpgrade.getIosVersion();
		}
		boolean isOk = false;
		// 如果当前原生版本比至少版本都要小，则必须进行(原生安装)升级
		if (StringUtil.isBlank(nativeVer) || StringUtil.isNewVersion(nativeVer, leastNativeVersion)) {
			isOk = true;
		}
		if (StringUtil.isBlank(nativeVer) || StringUtil.isNewVersion(nativeVer, nativeVersion)) {
			isOk = true;
		}
		if (StringUtil.isBlank(contentVer) || StringUtil.isNewVersion(contentVer, updVersion)) {
			isOk = true;
		}
		if (isOk == true) {
			result.setCode(RespEnum.OK.getCode());
			result.put("pubUpgradeVo", getUpgradeByPlatform(platform));
		} else {
			result.setCode(RespEnum.PARAMERROR.getCode());
			result.addErr("当前版本不需要升级");
		}
		return result;
	}

	@Override
	public PubUpgradeVo getUpgradeByPlatform(int platform) {
		if (!PubPlatformEnum.isInScope(platform)) {
			return new PubUpgradeVo();
		}
		PubUpgrade pubUpgrade = this.getCachePubUpgrade();
		String marketFile = pubUpgrade.getAndroidMarketFile();
		String updVersion = pubUpgrade.getAndroidVersion();
		String updFile = pubUpgrade.getAndroidUpdFile();
		if (platform == PubPlatformEnum.ANDROID.getCode()) {
			marketFile = pubUpgrade.getAndroidMarketFile();
			updVersion = pubUpgrade.getAndroidVersion();
			updFile = pubUpgrade.getAndroidUpdFile();
		} else if (platform == PubPlatformEnum.IOS.getCode()) {
			marketFile = pubUpgrade.getIosMarketFile();
			updVersion = pubUpgrade.getIosVersion();
			updFile = pubUpgrade.getIosUpdFile();
		} else {
			return new PubUpgradeVo();
		}
		PubUpgradeVo pubUpgradeVo = new PubUpgradeVo();
		pubUpgradeVo.setIsUpgrade(pubUpgrade.getIsUpgrade());
		pubUpgradeVo.setIsRestart(pubUpgrade.getIsRestart());
		pubUpgradeVo.setIsSilence(pubUpgrade.getIsSilence());
		pubUpgradeVo.setIsWebCheck(pubUpgrade.getIsWebCheck());
		pubUpgradeVo.setTitle(pubUpgrade.getTitle());
		pubUpgradeVo.setContent(pubUpgrade.getContent());
		pubUpgradeVo.setContentList(pubUpgrade.getContentList());
		pubUpgradeVo.setNativeVersion(pubUpgrade.getNativeVersion());
		pubUpgradeVo.setNativeBuild(pubUpgrade.getNativeBuild());
		pubUpgradeVo.setLeastNativeVersion(pubUpgrade.getLeastNativeVersion());
		pubUpgradeVo.setMarketFile(marketFile);
		pubUpgradeVo.setUpdVersion(updVersion);
		pubUpgradeVo.setIsUpdContentOnly(pubUpgrade.getIsUpdContentOnly());
		pubUpgradeVo.setUpdFile(updFile);
		pubUpgradeVo.setIsShowUpdContent(YesNoEnum.NO.getCode());
		return pubUpgradeVo;
	}
}